package com.qianfeng.rabbitmq.springbootconsumer.config;

//
//                            _ooOoo_  
//                           o8888888o  
//                           88" . "88  
//                           (| -_- |)  
//                            O\ = /O  
//                        ____/`---'\____  
//                      .   ' \\| |// `.  
//                       / \\||| : |||// \  
//                     / _||||| -:- |||||- \  
//                       | | \\\ - /// | |  
//                     | \_| ''\---/'' | |  
//                      \ .-\__ `-` ___/-. /  
//                   ___`. .' /--.--\ `. . __  
//                ."" '< `.___\_<|>_/___.' >'"".  
//               | | : `- \`.;`\ _ /`;.`/ - ` : | |  
//                 \ \ `-. \_ __\ /__ _/ .-` / /  
//         ======`-.____`-.___\_____/___.-`____.-'======  
//                            `=---='  
//  
//         .............................................  
//                  佛祖镇楼                  BUG辟易  
//          佛曰:  
//                  写字楼里写字间，写字间里程序员；  
//                  程序人员写程序，又拿程序换酒钱。  
//                  酒醒只在网上坐，酒醉还来网下眠；  
//                  酒醉酒醒日复日，网上网下年复年。  
//                  但愿老死电脑间，不愿鞠躬老板前；  
//                  奔驰宝马贵者趣，公交自行程序员。  
//                  别人笑我忒疯癫，我笑自己命太贱；  
//  


import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Created by Jackiechan on 2022/9/14 20:04
 *
 * @author Jackiechan
 * @version 1.0
 * @since 1.0
 */
@Configuration
public class RabbitConfig implements RabbitTemplate.ConfirmCallback, RabbitTemplate.ReturnsCallback {

    private RabbitTemplate rabbitTemplate;
    /**
     * 声明一个队列
     *
     * @return
     */
    @Bean
    public Queue queue() {
        return new Queue("onine11Simple", false, false, false, null);
    }

    /**
     * 声明交换机
     *
     * @return
     */
    @Bean
    public FanoutExchange fanoutExchange() {
        return new FanoutExchange("ONLINE11FANOUT", false, false);
    }

    /**
     * 声明和fanoutexchange进行绑定的队列
     *
     * @return
     */
    @Bean
    public Queue fanoutQueue() {
        return new Queue("fanoutQueue1", false, false, false, null);
    }

    /**
     * 绑定队列到交换机
     *
     * @param fanoutQueue
     * @param fanoutExchange
     * @return
     */
    @Bean
    public Binding bindQueue2Fanout(Queue fanoutQueue, FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(fanoutQueue).to(fanoutExchange);
    }

    @Bean
    public Queue directQueue1() {
        return new Queue("directQueue1", false, false, false, null);
    }

    @Bean
    public DirectExchange directExchange() {
        return new DirectExchange("ONLINE11DIRECT", false, false);
    }

    @Bean
    public Binding bindQueue2Direct(Queue directQueue1, DirectExchange directExchange) {
        return BindingBuilder.bind(directQueue1).to(directExchange).with("dingdan");
    }
    @Bean
    public Binding bindQueue2Direct2(Queue directQueue1, DirectExchange directExchange) {
        return BindingBuilder.bind(directQueue1).to(directExchange).with("dingdan222");
    }
    @Bean
    public Binding bindQueue2Direct3(Queue directQueue1, DirectExchange directExchange) {
        return BindingBuilder.bind(directQueue1).to(directExchange).with("dingdan333");
    }


    @Bean
    public TopicExchange topicExchange() {
        return new TopicExchange("ONLINE11topic", false, false);
    }


    @Autowired
    public void setRabbitTemplate(RabbitTemplate rabbitTemplate) {
        this.rabbitTemplate = rabbitTemplate;
        rabbitTemplate.setConfirmCallback(this);
        rabbitTemplate.setReturnsCallback(this);
    }


    @Override
    public void confirm(CorrelationData correlationData, boolean ack, String cause) {
        if (ack) {
            System.err.println("发送到交换机成功");
        }else{
            System.err.println("发送到交换机失败");
        }
    }

    @Override
    public void returnedMessage(ReturnedMessage returned) {
        System.err.println("消息退回来了");
    }
}
